package com.xiuyou.tracksdk.parse;


import com.xiuyou.tracksdk.http.ResponseParams;
import com.xiuyou.tracksdk.tracksdk.module.HistoryPoint;
import com.xiuyou.tracksdk.tracksdk.module.Point;

import org.json.JSONArray;
import org.json.JSONObject;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by lhy on 2016/6/23.
 */
public class HistoryParse extends ResponseParams{

    @Override
    public Object parse(String json) throws Exception {
        HistoryPoint historyPoint = new HistoryPoint();
        JSONObject jsonObject = new JSONObject(json);

        String total = jsonObject.getString("total");
        String message = jsonObject.getString("message");
        String distance = jsonObject.getString("distance");
        String entity_name = jsonObject.getString("entity_name");
        String status = jsonObject.getString("status");
        historyPoint.setMessage(message);
        historyPoint.setTotal(total);
        historyPoint.setDistance(distance);
        historyPoint.setEntity_name(entity_name);
        historyPoint.setStatus(status);

        List<Point> pointList = new ArrayList<Point>();
        JSONArray jsonArray = jsonObject.getJSONArray("points");
        if(jsonArray != null){
            int size = jsonArray.length();
            for(int i=0;i<size;i++){
                Point point = new Point();
                JSONObject jsonObj = jsonArray.getJSONObject(i);
                String p_create_time = jsonObj.getString("create_time");
                String p_entity_name = jsonObj.getString("entity_name");
                String p_longitude = jsonObj.getString("longitude");
                String p_latitude = jsonObj.getString("latitude");
                String p_loc_time = jsonObj.getString("loc_time");
                String speed = jsonObj.getString("speed");
                point.setEntity_name(p_entity_name);
                point.setCreate_time(p_create_time);
                point.setLoc_time(p_loc_time);
                point.setLongitude(p_longitude);
                point.setLatitude(p_latitude);
                point.setSpeed(speed);
                pointList.add(point);
//                JSONArray customArray = jsonObj.getJSONArray()
            }
            historyPoint.setSize(String.valueOf(size));
            historyPoint.setPoints(pointList);
        }
        return historyPoint;
    }
}
